﻿/* 
 * Copyright (c) 2010 Letwory Interactive
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * -- Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * -- Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * Original contributor: Nathan Letwory
 */

using System;

namespace VerseSharp
{
    public delegate void BitmapLayerCreateCallback(BitmapLayerCreateCommand command);
    public delegate void BitmapLayerDestroyCallback(BitmapLayerCreateCommand command);

    /// <summary>
    /// Command to create or destroy a bitmap layer.
    /// </summary>
    public class BitmapLayerCreateCommand: IVerseCommand
    {
        /// <summary>
        /// Protocol ID of the command.
        /// </summary>
        public CommandID ID { get { return CommandID.BitmapLayerCreate; } }
        /// <summary>
        /// ID of the node being accessed.
        /// </summary>
        public uint NodeID;
        /// <summary>
        /// ID of the layer. Set to ushort.MaxValue to create a new layer. Setting to
        /// a known ID will change that layer.
        /// </summary>
        public ushort LayerID;
        private string _name;
        /// <summary>
        /// Name of the layer. Truncated to 16 characters long. Set to null or empty if you
        /// want this command to destroy the specified layer.
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                if (value != null)
                {
                    if (value.Length <= 16)
                    {
                        _name = value;
                    }
                    else
                    {
                        _name = value.Substring(0, 16);
                    }
                }
            }
        }
        /// <summary>
        /// Type of the layer to create. This field has no meaning when destroying a layer.
        /// </summary>
        public BitmapLayerType Type;

        public void Deserialize(byte[] source, ref int pos)
        {
            NodeID = Utils.GetUInt32(source, ref pos);
            LayerID = Utils.GetUInt16(source, ref pos);
            _name = Utils.GetString(source, 16, ref pos);
            Type = (BitmapLayerType)Utils.GetUInt8(source, ref pos);
        }

        public void Serialize(byte[] destination, ref int pos)
        {
            Utils.PutUInt32(destination, ref pos, NodeID);
            Utils.PutUInt16(destination, ref pos, LayerID);
            Utils.PutString(destination, _name, 16, ref pos);
            Utils.PutUInt8(destination, ref pos, (byte)Type);
        }
    }
}
